<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "{{%gky_lunwen}}".
 *
 * @property string $id
 * @property string $lwno
 * @property string $sort
 * @property string $gdunit
 * @property string $gdno
 * @property string $lwname
 * @property string $lwname_en
 * @property string $lwtype
 * @property string $kwname
 * @property string $hxkwtype
 * @property string $issnno
 * @property string $cnno
 * @property string $address
 * @property string $iswork
 * @property string $publishdate
 * @property string $lwfbqk
 * @property string $reelno
 * @property string $numbertype
 * @property string $startpage
 * @property string $endpage
 * @property string $pagenum
 * @property string $logtime
 * @property string $wlk
 * @property string $gxjsly
 * @property string $limittime
 * @property string $authornum
 * @property string $discipline
 * @property string $isproject
 * @property string $projectids
 * @property string $xgxk
 * @property string $zdxk
 * @property string $updatetime
 * @property integer $isfinish
 * @property string $addman
 * @property string $addmanid
 * @property string $addtime
 * @property string $author
 * @property string $jsqk
 * @property string $kwlevel
 * @property string $mark
 */
class Lunwen extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%gky_lunwen}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['lwno','gdno','isfinish','publishdate', 'issnno', 'cnno', 'projectids'], 'safe'],
            [[ 'sort', 'addmanid','startpage', 'endpage', 'pagenum', 'authornum'], 'integer'],
            [[ 'gdunit',  'lwname_en', 'lwtype', 'kwname', 'hxkwtype', 'lwfbqk', 'reelno', 'numbertype', 'wlk', 'gxjsly', 'discipline', 'addman', 'jsqk', 'kwlevel'], 'string', 'max' => 64],
            [['logtime'], 'string', 'max' => 20],
            [['lwname'], 'string', 'max' => 255],
            [['address', 'author'], 'string', 'max' => 512],
            [['iswork', 'limittime', 'isproject'], 'string', 'max' => 32],
            [['xgxk', 'zdxk', 'mark'], 'string', 'max' => 1024],
            [['updatetime', 'addtime'],'safe'],
            //添加必填验证
            [['lwname'],'required']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'lwno' => '论文编号',
            'sort' => '本校排名',
            'gdunit' => '归档单位',
            'gdno' => '归档编号',
            'lwname' => '论文名称',
            'lwname_en' => '论文名称翻译',
            'lwtype' => '论文类型',
            'kwname' => '发表刊物名称',
            'hxkwtype' => '核心刊物类型',
            'issnno' => 'issn号',
            'cnno' => 'cn号',
            'address' => '所在地',
            'iswork' => '是否审核',
            'publishdate' => '发表时间',
            'lwfbqk' => '论文发表情况',
            'reelno' => '卷号',
            'numbertype' => '期别',
            'startpage' => '开始页码',
            'endpage' => '结束页码',
            'pagenum' => '页码数',
            'logtime' => '登记年度',
            'wlk' => '文理科',
            'gxjsly' => '高新技术领域',
            'limittime' => '档案保存期限',
            'authornum' => '作者总人数',
            'discipline' => '所属学科',
            'isproject' => '是否立项',
            'projectids' => '项目编号',
            'xgxk' => '相关学科',
            'zdxk' => '重点学科',
            'updatetime' => '更新时间',
            'isfinish' => '是否完成',
            'addman' => '添加人员',
            'addmanid' => 'Addmanid',
            'addtime' => '添加时间',
            'author' => '作者',
            'jsqk' => '检索情况',
            'kwlevel' => '刊物级别',
            'mark' => '备注',
        ];
    }

    public function beforeSave($insert){

        if (parent::beforeSave($insert)) {
            //添加修改人的信息 由于这部分没有必要自己填写 所以直接获取登录人的信息  在保存的时候设置就好
           $id=Yii::$app->user->identity->id;
           //添加人的id
           $this->addmanid=$id;
           //添加人的姓名
           $name=  GkyUser::find()->where(['id'=>$id])->one()->username;
           $this->addman=$name;
           //更新时间
           $time=date("Y-m-d");
           $this->updatetime=$time;
           //添加时间
           $this->addtime=$time;
            return true;
        } else {
            return false;
        }
    }

    /**
     * 查询论文列表，返回字段为论文名，作者，添加时间
     *
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function getList($pageNow, $pageSize){

        $list = Lunwen::find()
            ->addSelect(['id','lwname','author','addtime'])
            ->addOrderBy(['addtime' => SORT_DESC])
            ->offset(($pageNow - 1) * $pageSize)
            ->limit($pageSize)
            ->all();
        return $list;
    }
    
}
